<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://code.google.com/p/granizo" prefix="granizo"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%if (pageContext.getAttribute("username") == null){
    Object principal = org.springframework.security.core.context.SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    if (principal instanceof org.springframework.security.core.userdetails.UserDetails) {
        pageContext.setAttribute("username", ((org.springframework.security.core.userdetails.UserDetails)principal).getUsername());
    } else {
        pageContext.setAttribute("username", principal.toString());
    }
}%>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <link href="<c:url value="/"/>style.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <div id="container">
      <!-- header -->
      <div id="header">
        <div style="color:white; float:right; background:url(images/menu_enactive.gif)">
          <p><c:out value="${username}"/> | <a href='<c:url value="/"/>j_spring_security_logout'>Logout</a></p>
        </div>
        <div id="logo"><a href='<c:url value="/"/>'><span class="orange">Yana</span> online shop</a></div>
        <div id="menu">
          <ul>
            <li><a href='<c:url value="/"/>' class="active">home</a></li>
            <li><a href='<c:url value="/admin"/>' >adimn</a></li>
          </ul>
        </div>
      </div>
      <!--end header -->
      <!-- main -->
      <div id="main">
        <div id="content">
          <c:out value="${error}"/>
          <table border="0">
            <tr>
              <c:set var="wasNavigated" value="false"/>
              <c:forEach items="${categoriesNavigation}" var="c">
                <c:if test='${wasNavigated}'>
                  <td> > </td>
                </c:if>
                <td>
                  <a href='<c:url value="/category/${c.id}"/>'>
                    <c:out escapeXml="true" value="${c.name}"/>
                    <c:set var="wasNavigated" value="true"/>
                  </a>
                </td>
              </c:forEach>
            </tr>
          </table>
          <div id="text">
            <c:forEach items="${products}" var="p">
              <h1>
                <a href='<c:url value="/category/${category_id}/product/${p.id}"/>'>
                  <c:out escapeXml="true" value="${p.name}"/>
                </a>
              </h1>
              <p>
                <table>
                  <tr>
                    <td>
                      <a href='<c:url value="/product/${p.id}/image"/>' target="_blank">
                        <img src='<c:url value="/product/${p.id}/image"/>' width="100px" height="100px" 
                             alt='<c:out escapeXml="true" value="${p.name}"/>' title='<c:out escapeXml="true" value="${p.name}"/>'/>
                      </a>
                    </td>
                    <td valign="top">
                      <b><c:out escapeXml="true" value="${p.name}"/> </b> (<c:out value="${p.price}"/>)
                      - <c:out escapeXml="true" value="${p.description}"/>
                    </td>
                  </tr>
                </table>
              </p>
            </c:forEach>
            <c:if test="${product != null}">
              <h1>
                <a href='<c:url value="/category/${category_id}/product/${p.id}"/>'>
                  <c:out escapeXml="true" value="${p.name}"/>
                </a>
              </h1>
              <p>
                <table>
                  <tr>
                    <td>
                      <a href='<c:url value="/product/${product.id}/image"/>' target="_blank">
                        <img src='<c:url value="/product/${product.id}/image"/>' width="100px" height="100px" 
                             alt='<c:out escapeXml="true" value="${product.name}"/>' title='<c:out value="${product.name}"/>'/>
                      </a>
                    </td>
                    <td valign="top">
                      <b><c:out escapeXml="true" value="${product.name}"/> </b> (<c:out value="${p.price}"/>)
                      - <c:out escapeXml="true" value="${product.description}"/>
                    </td>
                  </tr>
                </table>
              </p>
              <table border="1">
                <c:forEach items="${productComments}" var="c">
                  <fieldset>
                    <legend><c:out value="${c.date}"/></legend>
                    <p><c:out escapeXml="true" value="${c.description}"/></p>
                  </fieldset>
                </c:forEach>
              </table>
              <fieldset>
                <legend>Add comment</legend>
                <form action='<c:url value="/category/${category_id}/product/${product.id}/add/comment"/>'
                      method="POST">
                  <table>
                    <tr><td><textarea name="description"></textarea></td></tr>
                    <tr><td><input type="submit" value="Add comment"/></td></tr>
                  </table>
                </form>
              </fieldset>
            </c:if>
          </div>
          <div id="sidebar">
            <h2>Categories</h2>
            <ul>
              <c:set var="previousLevel" value="0"/>
              <c:if test="${not empty categoriesTree}">
                <granizo:tree tree="${categoriesTree}" connectMethod="getChilds" var="status">
                  <granizo:startElement>
                    <c:if test="${previousLevel < status.level}">
                      <ul>
                    </c:if>
                      <li>
                      <c:set var="showLink" value="${status.element.id.toString() != category_id}"/>
                      <c:if test="${showLink}">
                        <a href='<c:url value="/category/${status.element.id}"/>'>
                      </c:if>
                      ${status.element.name}
                      <c:if test="${showLink}">
                        </a>
                      </c:if>
                  </granizo:startElement>

                  <granizo:endElement>
                    <c:if test="${previousLevel > status.level}">
                      </ul>
                    </c:if>
                      </li>
                  </granizo:endElement>

                  <c:set var="previousLevel" value="${status.level}"/>
                </granizo:tree>
              </c:if>
            </ul>
          </div>
        </div>
      </div>
      <!-- end main -->

      <!-- footer -->
      <div id="footer">
        <div id="left_footer">&copy; Copyright 2013 Yana Potochniak</div>
        <div id="right_footer">
          <!-- Please do not change or delete this link. Read the license! Thanks. :-) -->
          Design by <a href="http://www.realitysoftware.ca" title="Website Design">Reality Software</a>
        </div>
      </div>
      <div style="display:none;"><a href="http://free-templates.ru">free-templates.ru</a></div>
        <!-- end footer -->
      </div>
    </body>
  </html>
